import java.util.*;

interface DarrTravel {
    public int objOp(Object obj, Object userArgs);
}

class Student implements DarrTravel {
    private String name = new String();
    private int age = 0;

    public Student() {}
    public Student(String name, int age) {
        set(name, age);
    }

    public Student set(String name, int age) {
        this.name = name;
        this.age = age;
        return this;
    }

    public Student show() {
        System.out.printf("name: %s, age = %d\n", name, age);
        return this;
    }

    public String toString() {
        return new String().format("name: %s, age = %d", name, age);
    }

    public int objOp(Object obj, Object Args) {
        Student stu = (Student)obj;
        stu.show();
        return 0;
    }
}

public class DarrTest {
    static final Scanner sc = new Scanner(System.in);

    //纯手工打造动态数组
    static public void main1(String [] args) {
        int num, count = 0;
        int [] arr = null;
        int [] prev = null;

        while (true) {
            System.out.printf("请输入数据(-1: 退出): ");
            num = sc.nextInt();
            if (num == -1)
                break;
            arr = new int[count + 1];
            if (prev != null)
                System.arraycopy(prev, 0, arr, 0, count);
            arr[count++] = num;
            prev = arr;
        }

        for (int i : arr) {
            System.out.printf("%d ", i);
        }
        System.out.println();
    }

    //使用整型态数组封装类
    static public void main2(String [] args) {
        int num;
        DarrInt darr = new DarrInt();

        while (true) {
            System.out.printf("请输入数据(-1: 退出): ");
            num = sc.nextInt();
            if (num == -1)
                break;
            darr.append(num);
        }

        darr.travel();
    }

    //使用整型态数组封装类
    static public void main(String [] args) {
        // int num;
        // char num;
        // double num;
        // String num;
        Student stu = null;
        String name;
        Darr darr = new Darr();

        while (true) {
            System.out.printf("请输入数据(-1: 退出): ");
            // num = sc.nextInt();
            // num = sc.next().charAt(0);
            // num = sc.nextDouble();
            name = sc.nextLine();
            // if (num == -1)
            // if (num == 'q')
            // if (num > -0.0001 && num < 0.0001)
            // if (num.equals("exit"))
            if (name.equals("exit"))
                break;
            /*
             * stu = new Student();
             * darr.append(stu.set(name, 23));
             */
            // darr.append(new Student(name, 23));
            darr.prepend(new Student(name, 23));
        }
        // darr.insert(3, new Student("插班生", 16));
        Object [] stus = {new Student("111", 11), new Student("222", 22), new Student("3333", 33)};
        darr.insert(3, stus);
        // darr.insert(3, new Student("111", 11), new Student("222", 22), new Student("3333", 33));
        System.out.printf("len = %d\n", darr.len());

        // darr.travel(stu, null);
        darr.travel(new DarrTravel() {
            public int objOp(Object obj, Object Args) {
                Student stu = (Student)obj;
                stu.show();
                return 0;
            }}, null);
    }
}
